package edu.cqie.smartfarm.controller;


import edu.cqie.smartfarm.constant.CommonConstant;
import edu.cqie.smartfarm.constant.UserConstant;
import edu.cqie.smartfarm.dto.LoginDTO;
import edu.cqie.smartfarm.dto.UserDTO;
import edu.cqie.smartfarm.dto.UserQueryDTO;
import edu.cqie.smartfarm.dto.UserUpdateDTO;
import edu.cqie.smartfarm.result.PageVO;
import edu.cqie.smartfarm.result.R;
import edu.cqie.smartfarm.service.IUserService;
import edu.cqie.smartfarm.utils.ThreadLocalUtil;
import edu.cqie.smartfarm.verify.anno.AdminRequired;
import edu.cqie.smartfarm.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yyw
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "用户管理")
public class UserController {
    private final IUserService userService;

    /**
     * 用户登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public R login(@Validated @RequestBody LoginDTO loginDTO) {
        String token = userService.login(loginDTO);
        return R.Success(token);
    }

    /**
     * 用户登出
     *
     * @param token
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation(value = "用户登出")
    public R logout(@RequestHeader("Authorization") String token) {
        String username = userService.logout(token);
        return R.Success(username + UserConstant.LOGOUT_SUCCESS);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取当前登录用户信息")
    public R info() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get(CommonConstant.USER_ID);
        UserVO userVO = userService.getByUserId(userId);
        return R.Success(userVO);
    }

    /**
     * 根据用户名获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation(value = "根据用户id获取用户信息")
    @AdminRequired
    public R<UserVO> getByUsername(@RequestParam Integer userId) {
        UserVO userVO = userService.getByUserId(userId);
        return R.Success(userVO);
    }

    /**
     * 添加用户
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加用户")
    @AdminRequired
    public R add(@Validated @RequestBody UserDTO userDTO) {
        userService.add(userDTO);
        return R.Success();
    }

    /**
     * 获取用户列表
     *
     * @param userQueryDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取用户列表")
    @AdminRequired
    public R<PageVO> list(UserQueryDTO userQueryDTO) {
        PageVO<UserVO> list = userService.pageList(userQueryDTO);
        return R.Success(list);
    }

    /**
     * 更新用户信息
     *
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息")
    @AdminRequired
    public R update(@Validated @RequestBody UserUpdateDTO userUpdateDTO) {
        userService.updateInfo(userUpdateDTO);
        return R.Success();
    }
}
